__init__.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. import functools
  2. import re
  3. import string
  4. import typing as t
  5. if t.TYPE_CHECKING:
  6. import typing_extensions as te
  7. class HasHTML(te.Protocol):
  8. def __html__(self) -> str:
  9. pass
  10. __version__ = "2.1.1"
  11. _strip_comments_re = re.compile(r"<!--.*?-->")
  12. _strip_tags_re = re.compile(r"<.*?>")
  13. def _simple_escaping_wrapper(name: str) -> t.Callable[..., "Markup"]:
  14. orig = getattr(str, name)
  15. @functools.wraps(orig)
  16. def wrapped(self: "Markup", *args: t.Any, **kwargs: t.Any) -> "Markup":
  17. args = _escape_argspec(list(args), enumerate(args), self.escape) # type: ignore
  18. _escape_argspec(kwargs, kwargs.items(), self.escape)
  19. return self.__class__(orig(self, *args, **kwargs))
  20. return wrapped
  21. class Markup(str):
  22. """A string that is ready to be safely inserted into an HTML or XML
  23. document, either because it was escaped or because it was marked
  24. safe.
  25. Passing an object to the constructor converts it to text and wraps
  26. it to mark it safe without escaping. To escape the text, use the
  27. :meth:`escape` class method instead.
  28. >>> Markup("Hello, <em>World</em>!")
  29. Markup('Hello, <em>World</em>!')
  30. >>> Markup(42)
  31. Markup('42')
  32. >>> Markup.escape("Hello, <em>World</em>!")
  33. Markup('Hello &lt;em&gt;World&lt;/em&gt;!')
  34. This implements the ``__html__()`` interface that some frameworks
  35. use. Passing an object that implements ``__html__()`` will wrap the
  36. output of that method, marking it safe.
  37. >>> class Foo:
  38. ... def __html__(self):
  39. ... return '<a href="/foo">foo</a>'
  40. ...
  41. >>> Markup(Foo())
  42. Markup('<a href="/foo">foo</a>')
  43. This is a subclass of :class:`str`. It has the same methods, but
  44. escapes their arguments and returns a ``Markup`` instance.
  45. >>> Markup("<em>%s</em>") % ("foo & bar",)
  46. Markup('<em>foo &amp; bar</em>')
  47. >>> Markup("<em>Hello</em> ") + "<foo>"
  48. Markup('<em>Hello</em> &lt;foo&gt;')
  49. """
  50. __slots__ = ()
  51. def __new__(
  52. cls, base: t.Any = "", encoding: t.Optional[str] = None, errors: str = "strict"
  53. ) -> "Markup":
  54. if hasattr(base, "__html__"):
  55. base = base.__html__()
  56. if encoding is None:
  57. return super().__new__(cls, base)
  58. return super().__new__(cls, base, encoding, errors)
  59. def __html__(self) -> "Markup":
  60. return self
  61. def __add__(self, other: t.Union[str, "HasHTML"]) -> "Markup":
  62. if isinstance(other, str) or hasattr(other, "__html__"):
  63. return self.__class__(super().__add__(self.escape(other)))
  64. return NotImplemented
  65. def __radd__(self, other: t.Union[str, "HasHTML"]) -> "Markup":
  66. if isinstance(other, str) or hasattr(other, "__html__"):
  67. return self.escape(other).__add__(self)
  68. return NotImplemented
  69. def __mul__(self, num: "te.SupportsIndex") -> "Markup":
  70. if isinstance(num, int):
  71. return self.__class__(super().__mul__(num))
  72. return NotImplemented
  73. __rmul__ = __mul__
  74. def __mod__(self, arg: t.Any) -> "Markup":
  75. if isinstance(arg, tuple):
  76. # a tuple of arguments, each wrapped
  77. arg = tuple(_MarkupEscapeHelper(x, self.escape) for x in arg)
  78. elif hasattr(type(arg), "__getitem__") and not isinstance(arg, str):
  79. # a mapping of arguments, wrapped
  80. arg = _MarkupEscapeHelper(arg, self.escape)
  81. else:
  82. # a single argument, wrapped with the helper and a tuple
  83. arg = (_MarkupEscapeHelper(arg, self.escape),)
  84. return self.__class__(super().__mod__(arg))
  85. def __repr__(self) -> str:
  86. return f"{self.__class__.__name__}({super().__repr__()})"
  87. def join(self, seq: t.Iterable[t.Union[str, "HasHTML"]]) -> "Markup":
  88. return self.__class__(super().join(map(self.escape, seq)))
  89. join.__doc__ = str.join.__doc__
  90. def split( # type: ignore
  91. self, sep: t.Optional[str] = None, maxsplit: int = -1
  92. ) -> t.List["Markup"]:
  93. return [self.__class__(v) for v in super().split(sep, maxsplit)]
  94. split.__doc__ = str.split.__doc__
  95. def rsplit( # type: ignore
  96. self, sep: t.Optional[str] = None, maxsplit: int = -1
  97. ) -> t.List["Markup"]:
  98. return [self.__class__(v) for v in super().rsplit(sep, maxsplit)]
  99. rsplit.__doc__ = str.rsplit.__doc__
  100. def splitlines(self, keepends: bool = False) -> t.List["Markup"]: # type: ignore
  101. return [self.__class__(v) for v in super().splitlines(keepends)]
  102. splitlines.__doc__ = str.splitlines.__doc__
  103. def unescape(self) -> str:
  104. """Convert escaped markup back into a text string. This replaces
  105. HTML entities with the characters they represent.
  106. >>> Markup("Main &raquo; <em>About</em>").unescape()
  107. 'Main » <em>About</em>'
  108. """
  109. from html import unescape
  110. return unescape(str(self))
  111. def striptags(self) -> str:
  112. """:meth:`unescape` the markup, remove tags, and normalize
  113. whitespace to single spaces.
  114. >>> Markup("Main &raquo;\t<em>About</em>").striptags()
  115. 'Main » About'
  116. """
  117. # Use two regexes to avoid ambiguous matches.
  118. value = _strip_comments_re.sub("", self)
  119. value = _strip_tags_re.sub("", value)
  120. value = " ".join(value.split())
  121. return Markup(value).unescape()
  122. @classmethod
  123. def escape(cls, s: t.Any) -> "Markup":
  124. """Escape a string. Calls :func:`escape` and ensures that for
  125. subclasses the correct type is returned.
  126. """
  127. rv = escape(s)
  128. if rv.__class__ is not cls:
  129. return cls(rv)
  130. return rv
  131. for method in (
  132. "__getitem__",
  133. "capitalize",
  134. "title",
  135. "lower",
  136. "upper",
  137. "replace",
  138. "ljust",
  139. "rjust",
  140. "lstrip",
  141. "rstrip",
  142. "center",
  143. "strip",
  144. "translate",
  145. "expandtabs",
  146. "swapcase",
  147. "zfill",
  148. ):
  149. locals()[method] = _simple_escaping_wrapper(method)
  150. del method
  151. def partition(self, sep: str) -> t.Tuple["Markup", "Markup", "Markup"]:
  152. l, s, r = super().partition(self.escape(sep))
  153. cls = self.__class__
  154. return cls(l), cls(s), cls(r)
  155. def rpartition(self, sep: str) -> t.Tuple["Markup", "Markup", "Markup"]:
  156. l, s, r = super().rpartition(self.escape(sep))
  157. cls = self.__class__
  158. return cls(l), cls(s), cls(r)
  159. def format(self, *args: t.Any, **kwargs: t.Any) -> "Markup":
  160. formatter = EscapeFormatter(self.escape)
  161. return self.__class__(formatter.vformat(self, args, kwargs))
  162. def __html_format__(self, format_spec: str) -> "Markup":
  163. if format_spec:
  164. raise ValueError("Unsupported format specification for Markup.")
  165. return self
  166. class EscapeFormatter(string.Formatter):
  167. __slots__ = ("escape",)
  168. def __init__(self, escape: t.Callable[[t.Any], Markup]) -> None:
  169. self.escape = escape
  170. super().__init__()
  171. def format_field(self, value: t.Any, format_spec: str) -> str:
  172. if hasattr(value, "__html_format__"):
  173. rv = value.__html_format__(format_spec)
  174. elif hasattr(value, "__html__"):
  175. if format_spec:
  176. raise ValueError(
  177. f"Format specifier {format_spec} given, but {type(value)} does not"
  178. " define __html_format__. A class that defines __html__ must define"
  179. " __html_format__ to work with format specifiers."
  180. )
  181. rv = value.__html__()
  182. else:
  183. # We need to make sure the format spec is str here as
  184. # otherwise the wrong callback methods are invoked.
  185. rv = string.Formatter.format_field(self, value, str(format_spec))
  186. return str(self.escape(rv))
  187. _ListOrDict = t.TypeVar("_ListOrDict", list, dict)
  188. def _escape_argspec(
  189. obj: _ListOrDict, iterable: t.Iterable[t.Any], escape: t.Callable[[t.Any], Markup]
  190. ) -> _ListOrDict:
  191. """Helper for various string-wrapped functions."""
  192. for key, value in iterable:
  193. if isinstance(value, str) or hasattr(value, "__html__"):
  194. obj[key] = escape(value)
  195. return obj
  196. class _MarkupEscapeHelper:
  197. """Helper for :meth:`Markup.__mod__`."""
  198. __slots__ = ("obj", "escape")
  199. def __init__(self, obj: t.Any, escape: t.Callable[[t.Any], Markup]) -> None:
  200. self.obj = obj
  201. self.escape = escape
  202. def __getitem__(self, item: t.Any) -> "_MarkupEscapeHelper":
  203. return _MarkupEscapeHelper(self.obj[item], self.escape)
  204. def __str__(self) -> str:
  205. return str(self.escape(self.obj))
  206. def __repr__(self) -> str:
  207. return str(self.escape(repr(self.obj)))
  208. def __int__(self) -> int:
  209. return int(self.obj)
  210. def __float__(self) -> float:
  211. return float(self.obj)
  212. # circular import
  213. try:
  214. from ._speedups import escape as escape
  215. from ._speedups import escape_silent as escape_silent
  216. from ._speedups import soft_str as soft_str
  217. except ImportError:
  218. from ._native import escape as escape
  219. from ._native import escape_silent as escape_silent # noqa: F401
  220. from ._native import soft_str as soft_str # noqa: F401